811C - Vladik and Memorable Trip - CodeForces Solution


dp implementation *1900

Please click on ads to support us..

Python Code:

import sys
import io
import os
from bisect import bisect_left
from collections import defaultdict
from functools import lru_cache, reduce
from itertools import accumulate

BUFSIZE = 8192


class FastIO(io.IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = io.BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(io.IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


def input(): return sys.stdin.readline().rstrip('\r\n')


def read_int_list():
    return list(map(int, input().split()))


def read_int_tuple():
    return tuple(map(int, input().split()))


def read_int():
    return int(input())



if 'AW' in os.environ.get('COMPUTERNAME', ''):
            f = open('inputs', 'r')
    def input():
        return f.readline().rstrip("\r\n")

n = read_int()
nums = read_int_list()
rg = defaultdict(lambda : [n, -1])

for i, x in enumerate(nums):
    if rg[x][0] > i: rg[x][0] = i
    if rg[x][1] < i: rg[x][1] = i

value = [[0] * n for _ in range(n)]
for i in range(n):
    ss, cur = {nums[i]}, nums[i]
    lo, hi = rg[nums[i]]
    if lo == i == hi:
        value[i][i] = cur

    for j in range(i + 1, n):
        if nums[j] not in ss:
            cur ^= nums[j]
            ss.add(nums[j])
        l, r = rg[nums[j]]
        if lo > l: lo = l
        if hi < r: hi = r
        if lo < i:
            break
        if lo == i and hi == j:
            value[i][j] = cur

del rg

dp = [0] + [0] * n
for j in range(n):
    dp[j] = dp[j - 1]
    for i in range(j + 1):
        tr = dp[i - 1] + value[i][j]
        if dp[j] < tr: dp[j] = tr

print(dp[n-1])


Comments

Submit
0 Comments
More Questions

1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization